package com.itdct.onflow.generator.template.web

import com.itdct.onflow.generator.config.GeneratorConfig
import com.itdct.onflow.generator.model.ClassModel
import com.itdct.onflow.generator.model.CompareResultModel
import com.itdct.onflow.generator.model.FieldModel

/**
 * @author Zhouwx
 * @date 2025/8/26 11:19:25
 * @version 1.0
 * @description
 */
class VueIndexTemplate(classModel: ClassModel, generatorConfig: GeneratorConfig) : BaseVueTemplate(classModel, generatorConfig) {
    override fun restPath(): String {
        return classModel.className + "/index.ts"
    }

    override fun render(): String {
        return """
import request from '@/axios'
import { ${classModel.className}DetailVo, ${classModel.className}EditQo, ${classModel.className}ListVo, ${classModel.className}SearchQo } from './types'
import { BaseDeleteQo } from '@/base/api/BaseQo'
import { KeyLabelValueVo } from '@/base/api/KeyLabelValueVo'

export const ${classModel.classNameLowCamel}PageApi = (data: ${classModel.className}SearchQo): Promise<IResponse<Array<${classModel.className}ListVo>>> => {
  return request.post({ url: '/${classModel.classNameLowCamel}/page.adw', data })
}

export const ${classModel.classNameLowCamel}DetailApi = (id: number): Promise<IResponse<${classModel.className}DetailVo>> => {
  const data = { id }
  return request.post({
    url: '/${classModel.classNameLowCamel}/detail.adw',
    data
  })
}

export const ${classModel.classNameLowCamel}AddApi = (data: ${classModel.className}EditQo): Promise<IResponse<number>> => {
  return request.post({ url: '/${classModel.classNameLowCamel}/add.adw', data })
}

export const ${classModel.classNameLowCamel}UpdateApi = (data: ${classModel.className}EditQo): Promise<IResponse<void>> => {
  return request.post({ url: '/${classModel.classNameLowCamel}/update.adw', data })
}

export const ${classModel.classNameLowCamel}DeleteApi = (data: BaseDeleteQo): Promise<IResponse<void>> => {
  return request.post({ url: '/${classModel.classNameLowCamel}/delete.adw', data })
}

${rangeStart("selectFunctions")}
${selectListFunctions()}
${rangeEnd("selectFunctions")}

        """.trimIndent()
    }

    private fun selectListFunctions(): String {
        return classModel.fieldList.filter {
            it.forSelect
        }.map {
            selectListContent(it)
        }.joinToString("\n")
    }

    private fun selectListContent(it: FieldModel): String {
        return """
|export const select${classModel.className}${it.nameBigCamel}ListApi = (
|  data: ${classModel.className}SearchQo
|): Promise<IResponse<Array<KeyLabelValueVo>>> => {
|  return request.post({ url: '/${classModel.classNameLowCamel}/select${it.nameBigCamel}List.${generatorConfig.apiSuffix}', data })
|}
        """.trimMargin()
    }

    override fun onModify(compareResultModel: CompareResultModel, content: String): String {
        var content = content
        val addForSelectFields = compareResultModel.addForSelect
        val addIndex = content.indexOf(rangeEnd("selectFunctions"))
        content = insertFieldListContent(content, addForSelectFields, addIndex, { index, fieldModel -> selectListContent(fieldModel) })

        val delForSelect = compareResultModel.delForSelect
        content = deleteRangeFunctionCode(content, "selectFunctions", delForSelect, { fieldModel -> selectListContent(fieldModel) })

        return content
    }

    override fun subModuleName(): String {
        return "api"
    }
}